<?php
namespace app\api\controller\yluser;
use x\my\AliyunApi;

class Userbank extends Base
{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //银行卡列表
    public function lists(){
        $d = $this->post;
        $rule = [];
        $msg = [];
        check($rule,$msg,$d);
        if ($d['type']=='') {
            $list = $this->db->query("select * from user_bank where user_id=? order by is_default desc,id desc",[$this->userinfo['id']]);
        }else{
            $list = $this->db->query("select * from user_bank where user_id=? and type=? order by is_default desc,id desc",[$this->userinfo['id'],$d['type']]);
        }

        $bank = $this->db->query("select * from com_bank",[]);
        $arr = [];
        foreach ($bank as $val) {
            $arr[$val['bank_name']] = $val;
        }
        foreach ($list as $k => $v) {
            if (isset($arr[$v['bank_name']])) {
                $list[$k]['img_info'] = $arr[$v['bank_name']];
            }else{
                $list[$k]['img_info'] = $arr['银联'];
            }
        }
        suc($list);
    }

    //验证银行卡
    public function getBankInfo(){
        $d = $this->post;
        $rule = [
            'bank_no'=>'require|min:16|max:19',
        ];
        $msg = [
            'bank_no.require' => '请输入银行卡号',
        ];
        check($rule,$msg,$d);

        $rs = AliyunApi::getBank($d['bank_no']);
        if (!$rs) err('远程请求失败');
        $rs = json_decode($rs,true);
        if ($rs['status']==0 && $rs['phone']!='') {
            suc($rs);
        }else{
            err('银行卡错误！');
        }

    }

    //绑定银行
    public function add(){
        $d = $this->post;
        $rule = [
            'bank_name'=>'require',
            'bank_no'=>'require',
            'code'=>'require',
        ];
        $msg = [
            'bank_name.require' => '请输入开户行',
            'bank_no.require' => '请输入银行卡号',
            'code.require' => '请输入短信验证码',
        ];
        check($rule,$msg,$d);

        //短信验证
        if(!m('sms','xcom')->checkCode($this->userinfo['phone'], 'bank', $d['code'])){
            err(m('sms','xcom')->getError());
        }

        $bank = $this->db->query("select * from user_bank where bank_no=?",[$d['bank_no']],1);
        if ($bank) err('该银行卡已被绑定!');

        $this->db->query("update user_bank set is_default=0 where user_id=?",[$this->userinfo['id']]);
        $data = [
            'user_id' => $this->userinfo['id'],
            'user_name' => $this->userinfo['name'],
            'type' => $d['type'],
            'bank_name' => $d['bank_name'],
            'bank_no' => $d['bank_no'],
            'bank_phone' => $d['bank_phone'],
            'create_time' => time(),
            'is_default' => 1
        ];
        $result = $this->db->insert("user_bank",$data);
        if(!$result) err('保存错误!');
        suc('保存成功!');
    }

    //设置默认
    public function default(){
        $d = $this->post;
        //判断短信验证码
        $rule = [
            'id'=>'require'
        ];
        $msg = [];
        check($rule,$msg,$d);
        $this->db->update("user_bank",["is_default"=>0],['user_id=?',[$this->userinfo['id']]]);
        $rs = $this->db->update("user_bank",["is_default"=>1],['user_id=? and id=?',[$this->userinfo['id'],$d['id']]]);
        if($rs){
            return suc('保存成功!');
        }else{
            err('保存错误!');
        }
    }

    //获取用户默认银行卡
    public function getDefault(){
        //用户地址信息获取
        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];
        $w = where($map);
        $info = $this->db->query("select * from user_bank  $w[0] order by is_default desc",$w[1],1);
        if (!$info) {
             suc();
        }

        $rs = $this->db->query("select * from com_bank where bank_name=?",[$info['bank_name']],1);
        if(!$rs){
            $rs = $this->db->query("select * from com_bank where id=?",[1],1);
        }
        $info['img_info'] = $rs;

        suc($info);
    }

    //删除
    public function del(){
        $id = $this->post("id");
        $info = $this->db->query('select * from user_bank where id=? and user_id=?',[$id,$this->userinfo["id"]]);
        if($info){
            $re = $this->db->delete('user_bank',$id,'id');
            if($re){
                return suc('删除成功!');
            }else{
                err('删除失败!');
            }
        }else{
            err('删除失败!');
        }
    }

}
